import {createRouter,createWebHashHistory} from 'vue-router' 
import { addAdminRoute } from './AdminRouter'
import { addUserRoute } from './UserRouter'
import { getCookies } from '@/utils/cookies'

export const routes = [
    {
        path:'/test',
        name:'test',
        component:() => import('../view/test.vue'),
    },
    {
        path:'/',
        name:'main',
        component:() => import('../layout/mian.vue'),
    },
    {
        path:'/login',
        name:'login',
        component:() => import('../view/login.vue'),
    },
    {
        path:'/addUser',
        name:'addUser',
        component:() => import('../view/addUser.vue'),
    }
]
const router = createRouter({
    history: createWebHashHistory(),
    routes: routes
})
router.beforeEach((to, from) => {
    if (!getCookies('state') && (to.name !== 'login' && to.name !== 'addUser')) {
        return { name:'login' }
    }
    if (!to.redirectedFrom) {
        if (getCookies('state') === 'admin') {
            addAdminRoute(router)
            return { ...to, replace: true }
        } else if (getCookies('state') === 'hotel'){
            addUserRoute(router)
            return { ...to, replace: true }
        }
      } else {
        return true
      }
})

export default router;